package com.py.credit.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.py.credit.common.XypjConstants;
import com.py.credit.entity.GatewayUserEntity;
import com.py.credit.service.GatewayDeclarationInformationService;
import com.py.credit.service.GatewayUserService;
import com.py.framework.core.common.Page;
import com.py.framework.core.rest.support.RestServiceResult;

/**
 * 信用评价前台网站信用申报Controller
 */
@RestController
@RequestMapping("/xypj/gateway")
public class GatewayDeclarationInformationController extends GatewaySystemController {

	@Autowired
	private GatewayDeclarationInformationService gatewayDeclarationInformationService;

	@Autowired
	private GatewayUserService gatewayUserService;

	/**
	 * 查询信用申报表信息菜单
	 *
	 * @param tybm
	 *            统一编码
	 * @param type
	 *            注册类型 C:法人 P：自然人
	 * @return
	 */
	@RequestMapping(value = "/getDeclarationInformationList.json", method = RequestMethod.POST)
	public RestServiceResult getDeclarationInformationList(
			@RequestParam(value = "tybm", defaultValue = "", required = true) String tybm,
			@RequestParam(value = "type", defaultValue = "", required = true) String type) {
		int rootId = 0;

		if ("C".equals(type)) {
			rootId = XypjConstants.XYPJ_FR_ROOT_ID;
		} else {
			rootId = XypjConstants.XYPJ_ZRR_ROOT_ID;
		}

		List<LinkedHashMap> map = gatewayDeclarationInformationService.selectDeclarationInformationList(tybm, rootId);
		return super.getSuccessRestServiceResult(map);
	}

	/**
	 * 查询表头字段
	 *
	 * @param resource_id
	 *            表名
	 * @return
	 */
	@RequestMapping(value = "/selectTableTitleList.json", method = RequestMethod.POST)
	public RestServiceResult selectTableTitleList(
			@RequestParam(value = "resource_id", defaultValue = "", required = true) String resource_id) {
		List<LinkedHashMap> map = gatewayDeclarationInformationService.selectTableTitleList(resource_id);
		return super.getSuccessRestServiceResult(map);
	}

	/**
	 * 查询信用申报列表
	 *
	 * @param tybm
	 *            统一编码
	 * @param resource_id
	 *            表名
	 * @param limit
	 *            一页多少行
	 * @param page
	 *            页数
	 * @return
	 */
	@RequestMapping(value = "/getList.json", method = RequestMethod.POST)
	public RestServiceResult selectListForPage(
			@RequestParam(value = "tybm", defaultValue = "", required = true) String tybm,
			@RequestParam(value = "resource_id", defaultValue = "", required = true) String resource_id,
			@RequestParam(value = "limit", defaultValue = "", required = true) String limit,
			@RequestParam(value = "page", defaultValue = "", required = true) String page) {
		Page p = new Page();
		p.setLimit(Integer.parseInt(limit));
		p.setPage(Integer.parseInt(page));
		List<LinkedHashMap> map = gatewayDeclarationInformationService.selectListForPage(tybm, resource_id, p);
		return super.getPageSuccessRestServiceResult(p.getTotalCount(), map);
	}

	/**
	 * 查询申报详情
	 *
	 * @param resource_id
	 *            表名
	 * @param id
	 *            表主键id
	 * @return
	 */
	@RequestMapping(value = "/getInfo.json", method = RequestMethod.POST)
	public RestServiceResult selectInfo(
			@RequestParam(value = "resource_id", defaultValue = "", required = true) String resource_id,
			@RequestParam(value = "id", defaultValue = "", required = true) String id) {
		return super.getSuccessRestServiceResult(gatewayDeclarationInformationService.selectInfo(resource_id, id));
	}

	/**
	 * 新增申报信息
	 *
	 * @param resource_id
	 *            表名
	 * @param userId
	 *            用户id
	 * @param data
	 *            json数据:[{ "columnEn":"英文字段名1", "columnCn":"中文字段名1",
	 *            "roleId":"规则id", "value":“字段值” }, { "columnEn":"英文字段名2",
	 *            "columnCn":"中文字段名2", "roleId":"规则id", "value":“字段值” }, {
	 *            "columnEn":"英文字段名3", "columnCn":"中文字段名3", "roleId":"规则id",
	 *            "value":“字段值” } ]
	 * @return
	 */
	@RequestMapping(value = "/addInfo.json", method = RequestMethod.POST)
	public RestServiceResult addInfo(
			@RequestParam(value = "resource_id", defaultValue = "", required = true) String resource_id,
			@RequestParam(value = "userId", defaultValue = "", required = true) String userId,
			@RequestParam(value = "data", defaultValue = "", required = true) String data) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", Long.parseLong(userId));
		GatewayUserEntity gatewayUserEntity = gatewayUserService.getUserByTwoPrincipal(params);
		return gatewayDeclarationInformationService.addInfo(gatewayUserEntity, resource_id, data);
	}

	/**
	 * 修改申报信息
	 *
	 * @param id
	 *            表主键id
	 * @param resource_id
	 *            表名
	 * @param data
	 *            json数据:[{ "columnEn":"英文字段名1", "columnCn":"中文字段名1",
	 *            "roleId":"规则id", "value":“字段值” }, { "columnEn":"英文字段名2",
	 *            "columnCn":"中文字段名2", "roleId":"规则id", "value":“字段值” }, {
	 *            "columnEn":"英文字段名3", "columnCn":"中文字段名3", "roleId":"规则id",
	 *            "value":“字段值” } ]
	 * @return
	 */
	@RequestMapping(value = "/modifyInfo.json", method = RequestMethod.POST)
	public RestServiceResult modifyInfo(@RequestParam(value = "id", defaultValue = "", required = true) String id,
			@RequestParam(value = "resource_id", defaultValue = "", required = true) String resource_id,
			@RequestParam(value = "data", defaultValue = "", required = true) String data) {
		return gatewayDeclarationInformationService.modifyInfo(id, resource_id, data);
	}

	/**
	 * 删除申报信息
	 *
	 * @param id
	 *            表主键id
	 * @param resource_id
	 *            表名
	 * @return
	 */
	@RequestMapping(value = "/delInfo.json", method = RequestMethod.POST)
	public RestServiceResult delInfo(@RequestParam(value = "id", defaultValue = "", required = true) String id,
			@RequestParam(value = "resource_id", defaultValue = "", required = true) String resource_id) {
		gatewayDeclarationInformationService.delInfo(id, resource_id);
		return super.getSuccessRestServiceResult();
	}

	/**
	 * 校验字段规则
	 *
	 * @param column_rule_id
	 *            校验规则id
	 * @param data
	 *            数据
	 * @param extend
	 * @return
	 */
	@RequestMapping(value = "/checkRule.json")
	public RestServiceResult<Object> checkRule(
			@RequestParam(defaultValue = "", required = true, value = "column_rule_id") String column_rule_id,
			@RequestParam(defaultValue = "", required = true, value = "data") String data,
			@RequestParam(defaultValue = "", required = true, value = "extend") String extend) {
		Map<String, Object> info = new HashMap<String, Object>();
		if (null != column_rule_id && !"".equals(column_rule_id)) {
			List<Map<String, String>> columnRuleList = gatewayDeclarationInformationService
					.selectColumnRuleList(Arrays.asList(column_rule_id.split(",")));

			for (Map<String, String> map : columnRuleList) {
				if (!gatewayDeclarationInformationService.checkRule(map.get("rule_package"), map.get("rule_method"),
						data, extend)) {
					info.put("flag", "N");
					info.put("msg", map.get("rule_prompt"));
					return super.getSuccessRestServiceResult(info);
				}
			}
		}

		info.put("flag", "Y");
		info.put("msg", "校验通过");

		return super.getSuccessRestServiceResult(info);
	}
}